package com.shell.arouter.demo.paint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.henanjianye.soon.communityo2o2.R;

class MyView extends View {

    private Paint mPaint;
    private Shader mShaer;
    private Bitmap bitmap;

    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        mPaint = new Paint();//初始化画笔
//        mPaint.setColor(Color.RED);//设置颜色
//        mPaint.setARGB(255,255,255,0);//设置Paint对应颜色，范围为0-255
//        mPaint.setAlpha(200);//设置alpha不透明度，范围0-255
        mPaint.setAntiAlias(true);//设置抗锯齿
        mPaint.setStyle(Paint.Style.FILL);//描边效果
//        mPaint.setStrokeWidth(4);//描边宽度
//        mPaint.setStrokeCap(Paint.Cap.ROUND);//设置描边为圆角效果
//        mPaint.setStrokeJoin(Paint.Join.MITER);//设置拐角效果
//        mPaint.setShader(new SweepGradient(200,200,Color.BLUE,Color.RED));//设置环形渲染器
//        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST));//设置图层混合模式
//        mPaint.setColorFilter(new LightingColorFilter(0x00ffff,0x000000));//设置颜色过滤器
//        mPaint.setFilterBitmap(true);//设置双线过滤器
//        mPaint.setMaskFilter(new BlurMaskFilter(10,BlurMaskFilter.Blur.NORMAL));//设置画笔遮罩滤镜，传入度数和样式
//        mPaint.setTextScaleX(2);//设置文本缩放倍数
//        mPaint.setTextSize(38);//设置字体大小
//        mPaint.setTextAlign(Paint.Align.CENTER);//设置字体对齐方式
//        mPaint.setUnderlineText(true);//设置下划线
//
//        String str = "我要成为Android高级工程师";
//        Rect rect = new Rect();
//        mPaint.getTextBounds(str,0,str.length(),rect);//测量文本大小，将文本大小信息存放在rect中
//        mPaint.measureText(str);//提取文本的宽
//        mPaint.getFontMetrics();//获取字体度量对象
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /**
         * Call this to create a new shader that will draw with a bitmap.
         *
         * @param bitmap 指定bitmap
         * @param tileX 设置x方向的规则
         * @param tileY 设置y方向的规则
         */
        mShaer = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
        mPaint.setShader(mShaer);
        canvas.drawCircle(250,250,250,mPaint);
    }
}
